
# -*- conding:utf-8 -*-

import docx
from docx import Document
from docx.shared import Inches

file=docx.Document(r"./英语E、F开头单词 音标.docx")



print('段落: ' +str(len(file.paragraphs)))
#
# for para in file.paragraphs:
#  print(para.text)

# for i in range(len(file.paragraphs)):
#     print("第 " +str(i ) +"段的内容是： " +file.paragraphs[i].text,'daxiao:{}'.format(len(file.paragraphs[i].text)))

# 分开首字母单词，存入字典，格式是：{'A':[]}

dict = {}
key = 'A'
for i in range(len(file.paragraphs)):
    if len(file.paragraphs[i].text) == 2:
        dict[file.paragraphs[i].text] = []
        key = file.paragraphs[i].text
        continue
    dict[key].append(file.paragraphs[i].text.split(' '))



# for i in dict:
#     print(dict[i])

cixing = {}
# 创建文档对象
document_write = docx.Document()

# 设置文档标题，中文要用unicode字符串
document_write.add_heading(u'我的一个新文档', 0)

for i in dict:
    # document_write.add_paragraph(i)
    list = dict[i]
    # print(list)
    for j in list:
        for y in j:
            if y.endswith('.'):
                # print(y)
                cixing[y] = ''
                continue

    for ci in cixing:
        # document_write.add_paragraph(ci)
        for h in list:
            # print(h)
            if ci in h:
                para = ' '.join(h)
                print(para)
                document_write.add_paragraph(para)
        document_write.add_page_break()
        # p5 = document_write.add_paragraph('.add_page_break()硬分页，即使文本未满')


document_write.save('小方.docx')


print(cixing)